package com.haier.npt.security.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.haier.npt.common.JsonResult;
import com.haier.npt.page.PageBean;
import com.haier.npt.security.cache.UserPermissionCacheService;
import com.haier.npt.security.domain.SysPermission;
import com.haier.npt.security.domain.SysRole;
import com.haier.npt.security.domain.SysRoleMemberRe;
import com.haier.npt.security.dto.SysRoleDTO;
import com.haier.npt.security.dto.SysRoleSystemDTO;
import com.haier.npt.security.resource.SysRoleResource;
import com.haier.npt.security.service.SysPermissionService;
import com.haier.npt.security.service.SysRoleService;
import com.haier.npt.sso.client.SysAppCacheClient;

import java.util.*;


/**
 * @Description:角色服务
 * @author: lixu
 * @date:   2018年7月13日 下午2:49:02   
 */
@RestController
public class SysRoleController implements SysRoleResource {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private UserPermissionCacheService userpermissionCacheService;

    @Autowired
    private SysAppCacheClient sysAppCacheClient;


    /**
     * 根据当前登录人查询当前登录人的权限
     * @param userId
     * @return
     */
    @RequestMapping(value = "/internal/members/permissions/listByUserId", method = RequestMethod.GET)
    @Override
    public JsonResult<List<SysPermission>> listPermissionByUserId(@RequestParam(value = "userId") Long userId, @RequestParam(value = "token") String token){
    	List<SysPermission> permissions = userpermissionCacheService.getPermissionFromCache(userId);
        if (null == permissions) {
        	List<Long> memberIds = new ArrayList<>();
        	memberIds.add(userId);
            permissions = sysRoleService.listPermissionByMemberIds(memberIds);
            userpermissionCacheService.savePermissionCache(permissions, userId);
        } 
        Set<SysPermission> set = new HashSet<>();
        set.addAll(permissions);
        permissions.clear();
        permissions.addAll(set);
        return JsonResult.success(permissions);
    }

    /**
     * 添加一个角色
     * @param sysRole  角色对象
     * @return
     */
    @RequestMapping(value = "/internal/roles/add", method = RequestMethod.POST)
    @Override
    public JsonResult<Long> addrole(@RequestBody SysRole sysRole){
    	return JsonResult.success(sysRoleService.addRole(sysRole));
    }
    
    /**
     * 添加一个管理员角色
     * @param sysRoleSystemDTO 角色信息
     * @return
     */
    @RequestMapping(value = "/internal/roles/addRoleSystem", method = RequestMethod.POST)
    @Override
    public JsonResult<Long> addRoleSystem(@RequestBody SysRoleSystemDTO sysRoleSystemDTO){
    	return JsonResult.success(sysRoleService.addRoleSystem(sysRoleSystemDTO));
    }
    /**
     * 根据角色编号删除角色
     *
     * @param roleId   角色id
     * @return
     */
    @RequestMapping(value = "/internal/roles/{roleId}/delete", method = RequestMethod.POST)
    @Override
    public JsonResult<Boolean> deleteRole(@PathVariable(value = "roleId") Long roleId){
    	return JsonResult.success(sysRoleService.deleteRole(roleId));
    }



    /**
     * 根据角色编号更新角色名
     *
     * @param roleId
     * @param roleName
     * @return
     */
    @RequestMapping(value = "/internal/roles/{roleId}/editName", method = RequestMethod.POST)
    @Override
    public JsonResult<Boolean> editRoleName(@PathVariable(value = "roleId") Long roleId,
                                     @RequestParam(value = "roleName") String roleName){
    	return JsonResult.success(this.sysRoleService.editRoleName(roleId, roleName));
    }


    /**
     * 给memberid 赋予多个角色
     * @param memberId 成员编号
     * @param roleIds  角色编号
     * @return
     */
    @RequestMapping(value = "/internal/members/grantRoleBatch", method = RequestMethod.POST)
    @Override
    public JsonResult<Boolean> addMemberRoles(@RequestParam(value = "memberId") Long memberId, @RequestParam(value = "roleIds[]") Long roleIds[]){
    	return JsonResult.success(this.sysRoleService.addMemberRoles(memberId, roleIds));
    }

    /**
     * 批量获取成员的所有角色
     *
     * @param memberIds
     * @return
     */
    @RequestMapping(value = "/internal/members/list", method = RequestMethod.POST)
    @Override
    public JsonResult<List<SysRoleDTO>> listMemberRoles(@RequestParam(value = "memberIds[]") Long memberIds[]){
    	return JsonResult.success(this.sysRoleService.listMemberRoles(memberIds));
    }


    /**
     * 角色添加成员
     *
     * @param roleId    角色编号
     * @param memberIds 成员编号
     * @return
     */
    @RequestMapping(value = "/internal/roles/{roleId}/members/addBatch", method = RequestMethod.POST)
    @Override
    public JsonResult<Boolean> addRoleMember(@PathVariable(value = "roleId") Long roleId, @RequestParam(value = "memberIds[]") Long memberIds[]){
    	return JsonResult.success(this.sysRoleService.addRoleMember(roleId, memberIds));
    }


    /**
     * 角色删除成员
     *
     * @param roleId    角色编号
     * @param memberIds 成员编号
     * @return
     */
    @RequestMapping(value = "/internal/roles/{roleId}/members/deleteBatch", method = RequestMethod.POST)
    @Override
    public JsonResult<Boolean> deleteRoleMember(@PathVariable(value = "roleId") Long roleId, @RequestParam(value = "memberIds[]") Long memberIds[]){
    	return JsonResult.success(this.sysRoleService.deleteRoleMember(roleId, memberIds));
    }

    /**
     * 根据角色编号查询此角色下的所有成员集合(分页)
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/internal/roles/{roleId}/members/list", method = RequestMethod.GET)
    @Override
    public JsonResult<PageBean<SysRoleMemberRe>> listMemberByRole(@PathVariable(value = "roleId") Long roleId,
                                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex){
    	return JsonResult.success(this.sysRoleService.listMemberByRole(roleId, pageSize, pageIndex));
    }

    /**
     * 根据成员编号删除对应的角色关系
     *
     * @param memberIds 成员编号
     * @return
     */
    @RequestMapping(value = "/internal/members/revokeAllRoleBatch", method = RequestMethod.POST)
    @Override
    public JsonResult<Boolean> deleteRoleMembers(@RequestParam(value = "memberIds[]") Long memberIds[]){
    	return JsonResult.success(this.sysRoleService.deleteRoleMembers(memberIds));
    }


    /**
     * 角色添加权限
     *
     * @param roleId         角色编号
     * @param sysPermissions 权限集合
     * @return
     */
    @RequestMapping(value = "/internal/roles/{roleId}/permissions/addBatch", method = RequestMethod.POST)
    @Override
    public JsonResult<Boolean> addRolePermission(@PathVariable(value = "roleId") Long roleId,
                                          @RequestBody List<SysPermission> sysPermissions){
    	return JsonResult.success(sysRoleService.addRolePermission(roleId, sysPermissions));
    }


    /**
     * 角色删除权限
     * @param roleId        角色编号
     * @param permissionIds 权限ID集合
     * @return
     */
    @RequestMapping(value = "/internal/roles/{roleId}/permissions/deletebatch", method = RequestMethod.POST)
    @Override
    public JsonResult<Boolean> deleteRolePermission(@PathVariable(value = "roleId") Long roleId, @RequestParam(value = "permissionIds[]") Long[] permissionIds){
    	return JsonResult.success(this.sysRoleService.deleteRolePermission(roleId, permissionIds));
    }

    /**
     * 根据角色ID查询关联的权限ID集合
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/internal/roles/{roleId}/permissions/listIds", method = RequestMethod.POST)
    @Override
    public JsonResult<List<Long>> listRolePermissionIds(@PathVariable(value = "roleId") Long roleId){
    	return JsonResult.success(this.sysRoleService.listRolePermissionIds(roleId));
    }



    /**
     * 根据角色ID查询角色信息 
     *
     * @param roleId   角色ID
     * @return
     */
    @RequestMapping(value = "/internal/roles/{roleId}/get", method = RequestMethod.GET)
    @Override
    public JsonResult<SysRole> getSysRoleById( @PathVariable(value = "roleId") Long roleId){
    	List<Long> roleIds = new ArrayList<>();
    	roleIds.add(roleId);
    	List<SysRole> sysRole = sysRoleService.getSysRolesByIds(roleIds);
    	return JsonResult.success(sysRole.isEmpty()?null:sysRole.get(0));
    }
}
